package com.cgbl.cg.ui.mine

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import com.cgbl.cg.BR
import com.cgbl.cg.R
import com.cgbl.cg.databinding.AppFragmentMineBinding
import com.cgbl.cg.view.ExchangeCodePop
import com.cgbl.cg.view.PersonalSignPop
import com.cgbl.cg.vm.mine.MineViewModel
import com.lxj.xpopup.XPopup
import com.robot.base.base.activity.GLBaseFragment
import com.robot.base.util.LiveDataEventManager

class MineFragment : GLBaseFragment<AppFragmentMineBinding, MineViewModel>() {

    private lateinit var signPop: PersonalSignPop

    private lateinit var exchangeCodePop: ExchangeCodePop

    override fun initContentView(
        inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?
    ): Int = R.layout.app_fragment_mine

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {
        initPop()
    }

    override fun initData() {
        super.initData()
        viewModel.refreshUserInfo()
    }

    private fun initPop() {
        signPop = PersonalSignPop(requireContext())
        exchangeCodePop = ExchangeCodePop(requireContext())
    }

    override fun initViewObservable() {
        viewModel.uc.showSignPop.observe(this) {
            XPopup.Builder(requireContext()).asCustom(signPop).show()
        }
        viewModel.uc.showExchangeCodePop.observe(this) {
            XPopup.Builder(requireContext()).asCustom(exchangeCodePop).show()
        }
        LiveDataEventManager.observerLoginSuccessEvent(this) {
            viewModel.refreshUserInfo()
        }
    }
}